package com.cfpl.admin_service.controller;

import com.cfpl.admin_service.DTO.LoginFormDTO;
import com.cfpl.admin_service.VO.AdminLoginVO;
import com.cfpl.admin_service.service.AdminService;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: LYP
 * @Date: 2023/10/12 - 10:13
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@Api(tags = "登录接口")
@RequestMapping("/backStage/admin")
public class LoginController  {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @param loginFormDTO 登录信息DTO
     * @return toke+username
     */
    @ApiOperation("管理员登录接口")
    @PostMapping("/login")
    public BaseResult<AdminLoginVO> adminLogin(@RequestBody LoginFormDTO loginFormDTO){
        log.info("********进入controller***********");

        AdminLoginVO adminLoginVO = adminService.verifyAdminInfo(loginFormDTO);
        return BaseResult.ok(adminLoginVO);
    }

    /**
     * 测试接口
     * @return
     */
    @ApiOperation("测试接口")
    @GetMapping("/getUser")
//    @PreAuthorize("hasAnyAuthority('/CultivatePersonnel/showAll')")
    //使用SPEL表达式指定自己定义的认证权限
    @PreAuthorize("@myExpressionRoot.hasAuthority('/CultivatePersonnel/showAll')")
    public BaseResult<String> getUser(){
        return BaseResult.ok("nihao");
    }


    /**
     * 退出登录接口
     * @return string
     */
    @ApiOperation("注销接口")
    @GetMapping("/logout")
    public BaseResult<String> logOut(HttpServletRequest request){
        adminService.logOut(request);
        return BaseResult.ok("注销成功");
    }



}
